package com.app.base.service.core.recruit.service.impl;

import com.app.base.config.exception.SaaSException;
import com.app.base.model.vo.RecruitSearchVo;
import com.app.base.model.vo.UsCoVo;
import com.app.base.service.auth.service.SysUserService;
import com.app.base.service.core.recruit.entity.RecruitDetail;
import com.app.base.service.core.recruit.mapper.RecruitDetailMapper;
import com.app.base.service.core.recruit.service.RecruitDetailService;
import com.app.base.utlis.result.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 招聘信息发布表 服务实现类
 * @author yuhag
 * @since 2022-05-16
 */
@Service
@Slf4j
public class RecruitDetailServiceImpl extends ServiceImpl<RecruitDetailMapper, RecruitDetail> implements RecruitDetailService {


    @Resource
    private SysUserService userService;

    @Override
    public Result pageRecruit(Long current, Long pageSize, RecruitSearchVo recruitSearchVo) {
        try{
            Page<RecruitDetail> page = new Page<>(current,pageSize);
            List<RecruitDetail> recruitDetailList = baseMapper.pageRecruit(recruitSearchVo,page);
            page.setRecords(recruitDetailList);
            return Result.ok(page);
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result saveOrUpdateRecruit(RecruitDetail recruit, HttpServletRequest request) {
        try{
            UsCoVo userCompanyInfo = userService.getUserCompanyInfo(request);
            if (Objects.isNull(recruit)){
                throw new SaaSException("招聘审核数据为空!");
            }
            recruit.setModifyBy(userCompanyInfo.getUserId());
            if (StringUtils.isEmpty(recruit.getId())){
                recruit.setCompanyId(userCompanyInfo.getCompanyId());
                recruit.setCreatedBy(userCompanyInfo.getUserId());
                recruit.setStatus(0);
                baseMapper.insert(recruit);
                return Result.ok().message("添加招聘审核数据成功");
            }else {
                baseMapper.updateById(recruit);
                return Result.ok().message("更新招聘审核数据成功");
            }

        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result getByIdRecruit(String id) {
        try{
            RecruitDetail recruitDetail = baseMapper.selectById(id);
            Date[] date = new Date[] {recruitDetail.getStartTime(),recruitDetail.getEndTime() };
            String[] ids = new String[] {recruitDetail.getDeptId(),recruitDetail.getPositionId()};
            recruitDetail.setOrgId(ids);
            recruitDetail.setRangeDate(date);
            return Result.ok(recruitDetail);
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result file(String id) {
        try{
            RecruitDetail recruitDetail = baseMapper.selectById(id);
            recruitDetail.setStatus(1);
            recruitDetail.setFilingTime(new Date());
            baseMapper.updateById(recruitDetail);
            return Result.ok().message("归档成功");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result del(String id) {
        try{
            baseMapper.deleteById(id);
            return Result.ok().message("删除成功");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

}
